package com.jader.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jader.util.JaderApplication;
import com.jader.util.MD5Tool;
import com.jader.util.PreferenceUtil;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

public class SetPassWordActivity extends Activity {
	Button btn_forget_finish;
	EditText editPassWord;
	private HttpUtils httpUtils;
	ImageView password_back;
	TextView password_title;
	String url = JaderApplication.getIP() + "/Jader/client";
	private String newPassWord;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_password);
		initView();
	}

	/**
	 * 初始化视图
	 */
	private void initView() {
		btn_forget_finish = (Button) findViewById(R.id.btn_forget_finish);
		editPassWord = (EditText) findViewById(R.id.edit_forget_password);
		password_title = (TextView) findViewById(R.id.top_actiobar_text);
		password_title.setText("设置新密码");
		password_back = (ImageView) findViewById(R.id.top_actionbar_back);
	}

	public void finish(View view) {
		SetPassWord();
	}

	private void SetPassWord() {
		Intent intent = getIntent();
		String phoneNum = intent.getStringExtra("phoneNum");
		newPassWord = MD5Tool.MD5(editPassWord.getText()
				.toString().trim());
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", ""
				+ JaderApplication.USER_CHANGEPASSWORD);
		params.addBodyParameter("newPassWord", newPassWord);
		params.addBodyParameter("phoneNum", phoneNum);
		httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						JaderApplication.show(getBaseContext(), "修改密码成功,请重新登录");
						PreferenceUtil.getInstance(getBaseContext())
								.cleanUserInfo();
						JaderApplication.user.setPassword(newPassWord);
						Intent login = new Intent(SetPassWordActivity.this,
								LoginActivity.class);
						startActivity(login);

					}
				});
	}
}
